package com.timepies;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.timepies.domain.Pie;
import com.timepies.util.Intervallo;
import com.timepies.util.IntervalloDataBuilder;

public class SettingsActivity extends Activity {

	private static final String TAG = SettingsActivity.class.getSimpleName();
	protected Intervallo interval = new Intervallo();
	private ListView mainListView;
	private int listId = R.layout.activity_settings;
	private SettingsAdapter listAdapter;
	private Thread threadRefresh;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pie_elenco);

		buildList();
		autoRefresh();
	}

	private void buildList() {
		try {
			InputStream dati = getAssets().open("dati.csv");
			IntervalloDataBuilder builder = new IntervalloDataBuilder(dati);
			List<Pie> pies = builder.getListPie();
			this.interval.setStart(new Date());
			this.mainListView = (ListView) findViewById(R.id.pie_main_list);
			this.listAdapter = new SettingsAdapter(this, listId, pies);
			this.mainListView.setAdapter(listAdapter);
			Log.d(TAG, "buildList complete.");
		} catch (Exception e) {
			Log.e(TAG, "error : " + e.getMessage());
			Log.e(TAG, "error : " + e.getCause());
		}
	}

	private void autoRefresh() {
		threadRefresh = new Thread() {

			@Override
			public void run() {
				try {
					while (!isInterrupted()) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								listAdapter.notifyDataSetChanged();
							}
						});
						Thread.sleep(1000);
					}
				} catch (InterruptedException e) {
				}
			}
		};
		threadRefresh.start();
	}

	class SettingsAdapter extends ArrayAdapter<Pie> {

		private final List<Pie> listPie;

		public SettingsAdapter(SettingsActivity activity, int rLayoutId,
				List<Pie> listPie) {
			super(activity, rLayoutId, listPie);
			this.listPie = listPie;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {

			LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.activity_settings, null);
			Pie pie = getItem(position);
			TextView tv = (TextView) view.findViewById(R.id.txt_testo);
			String key = pie.getNome();
			String valore = interval.printTxtSentence(pie, false);
			tv.setText(key + ":" + valore);

			int color = Pie.randomColor(position);
			LinearLayout l = (LinearLayout) view.findViewById(R.id.color);
			l.setBackgroundColor(color);

			return view;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.settings_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d(TAG, "MenuItem: " + item.getItemId());
		switch (item.getItemId()) {

		case R.id.action_about:
			startActivity(new Intent(SettingsActivity.this,
					SplashActivity.class));
			finish();
			break;

		case R.id.action_exit:
			exit();
			break;

		}
		return true;

	}

	@Override
	public void onBackPressed() {
		exit();
	};

	public void exit() {
		Log.d(TAG, "Exit");
		if (threadRefresh != null)
			threadRefresh.interrupt();
		Intent exit = new Intent(SettingsActivity.this, ElencoPieActivity.class);
		startActivity(exit);
		finish();
	}
}